package com.example.shoppingdemo.goods;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckedTextView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.shoppingdemo.R;
import com.example.shoppingdemo.goods.GoodsJieXi.Goodsjiexi;
import com.example.shoppingdemo.goods.model.BuycarModel;
import com.example.shoppingdemo.goods.model.GoodsModel;
import com.example.shoppingdemo.goods.model.SkuModel;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

public class Goods_classify extends AppCompatActivity {

    GoodsFlowLayout goodsFlowLayout,sizeFlowLayout;
    int type=0;
    Context context;
    List<String> lists;
    List<View> viewList=new ArrayList<>();
    String typed;
    ImageView linearBreak,goodsAdd,goodsRemove,goodsImg;
    EditText goodsNum;
    int num=0;
    int n = 0;
    Goodsjiexi goodsjiexi;
    String[] price;
    int index;
    TextView buycar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_classify);
        goodsFlowLayout = findViewById(R.id.gods_classify);

        goodsAdd = findViewById(R.id.goods_add_img);
        goodsRemove = findViewById(R.id.goods_remove_img);
        goodsNum = findViewById(R.id.goods_num);
        linearBreak = findViewById(R.id.goods_break);
        buycar = findViewById(R.id.text_gouwuche);

        goodsjiexi = new Goodsjiexi();
//        typed = getIntent().getStringExtra("type");
        //设置该Activity出场方式
        WindowManager m = getWindowManager();
        Display d = m.getDefaultDisplay(); // 为获取屏幕宽、高
        WindowManager.LayoutParams p = getWindow().getAttributes();
        p.height = (int) (d.getHeight() * 0.8); // 高度设置为屏幕的0.8
        p.width = d.getWidth(); // 宽度设置为屏幕的0.7
        p.gravity = Gravity.BOTTOM;
        getWindow().setAttributes(p);

        initData();
        iniClick();
    }

    private void iniClick(){

        goodsAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                n++;
                if (n>0){
                    goodsRemove.setVisibility(View.VISIBLE);
                }
                num = n;
                goodsNum.setText(num+"");
            }
        });

        goodsRemove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                n = Integer.parseInt(goodsNum.getText().toString());
                n--;
                if (n == 0){
                    goodsRemove.setVisibility(View.GONE);
                }
                num = n;
                goodsNum.setText(num+"");

            }
        });

        linearBreak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        buycar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goodsjiexi.AddGoodsResquest(1, 27, 98, 2, new Goodsjiexi.onAddcar() {
                    @Override
                    public void onAddmsg(List<BuycarModel> list) {
                        String msg = list.get(0).getMsg();
                        if (msg.equals("添加成功")){
                            Toast.makeText(Goods_classify.this, "添加成功", Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(Goods_classify.this, "添加失败", Toast.LENGTH_SHORT).show();
                        }

                    }
                });
            }
        });
    }

    private void initData(){

        goodsImg = findViewById(R.id.sel_img);
        TextView goodsbuy=findViewById(R.id.text_gouwuche);
        TextView money = findViewById(R.id.sel_money);
        goodsjiexi.FindGoodsResquest(27, new Goodsjiexi.ongoodslisetener() {
            @Override
            public void ongoodsmsg(List<GoodsModel> list, SkuModel skuModel) {
                String img = list.get(0).getPic();
                lists = new ArrayList<>();
                Glide.with(Goods_classify.this)
                        .load(img)
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                        .into(goodsImg);

                price =new String[skuModel.getPrice().length];

                for (int i = 0; i < skuModel.getPrice().length; i++) {
                    price[i] = skuModel.getPrice()[i];
                }

                for (int i = 0; i < skuModel.getSpData().length; i++) {
                    try {
                        JSONArray array = new JSONArray(skuModel.getSpData()[i]);
                        String strColor = (String) array.getJSONObject(0).get("value");
                        String strSize = (String) array.getJSONObject(1).get("value");
                        lists.add(strSize+"/"+strColor);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                goodsFlowLayout.setAlignByCenter(GoodsFlowLayout.AlienState.LEFT);
                goodsFlowLayout.setAdapter(lists, R.layout.item, new GoodsFlowLayout.ItemView() {
                    @Override
                    void getCover(Object item, GoodsFlowLayout.ViewHolder holder, View inflate, int position) {
                        holder.setText(R.id.text_list,lists.get(position));
                        viewList.add(holder.mConvertView);
                        holder.mConvertView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                type=position;
                                setCurrentItem(lists.get(position),viewList);
                                money.setText(price[index]);
                            }
                        });
                    }
                });
            }
        });

        goodsbuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//                if (getCurrentItem(viewList,lists)==null){
//                    Toast.makeText(Goods_classify.this, "请选择属性", Toast.LENGTH_SHORT).show();
//                }else {
//
//
//                }
            }
        });
    }
    public void setCurrentItem(String prodFormCd,List<View> list){
        //一行view一个list
        //多行就是list嵌套
        //是先循环行数，再循环每个行数里的view
        for (int i = 0; i < list.size(); i++) {
            ((CheckedTextView) list.get(i)).setChecked(false);
        }
        for (int i = 0; i < list.size(); i++) {
            ((CheckedTextView) list.get(type)).setChecked(true);
        }
    }
    public String getCurrentItem(List<View> list,List<String> slist){
        for (int i = 0; i < list.size(); i++) {
            if(((CheckedTextView) list.get(i)).isChecked()){
                index = i;
                return slist.get(i);
            }
        }
        return null;
    }
}